#include "xmakearchives.h"
#include "qrcutil.h"

/**
 * @brief XMakeArchives::XMakeArchives
 * 数据来源于 https://tboox.org
 * 数据使用 soup 清洗: XMake 部分
 *      li a[class="title"]
 */
XMakeArchives::XMakeArchives()
{
    foreach (auto line, QR(":/xmake-archives.txt").split("\n", Qt::SkipEmptyParts)) {
        auto title = line.mid(line.indexOf(">") + 1);
        title = title.mid(0, title.indexOf("</a>"));

        auto href = line.mid(line.indexOf("href=") + 6);
        href = href.mid(0, href.indexOf(">") - 1);

        add_archive(title, href);
    }
}

QString XMakeArchives::name()
{
    return "XMake 中文档案";
}

QString XMakeArchives::prefix()
{
    return "https://tboox.org";
}

QStringList XMakeArchives::searching(const QString &text)
{
    QStringList list;
    foreach (auto title, m_titles) {
        if (text.isEmpty() || title.contains(text, Qt::CaseInsensitive)) {
            list << title;
        }
    }
    return list;
}

QString XMakeArchives::content(const QString &text)
{
    return prefix() + m_archives.value(text);
}

void XMakeArchives::add_archive(QString title, QString link)
{
    m_titles << title;
    m_archives.insert(title, link);
}
